﻿using AutoMapper;
using MediatR;
using U.B.DoMain;
using U.B.ErrorCode;
using U.B.ReadApi.Command;
using U.B.ReadApi.Dto;
using U.B.Repistory;

namespace U.B.ReadApi.CommandHander
{
    public class GetRoleCommandHander : IRequestHandler<GetRoleCommand, ApiResult<List<RoleDto>>>
    {
        private readonly IRepistory<Role> _roleRepository;
        private readonly IMapper _mapper;
         public GetRoleCommandHander(IRepistory<Role> roleRepository, IMapper mapper)
        {
            _roleRepository = roleRepository;
            _mapper = mapper;
        }
        public Task<ApiResult<List<RoleDto>>> Handle(GetRoleCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<RoleDto>> result = new ApiResult<List<RoleDto>>();
            
            result.code = Renum.显示成功;
            result.Message = Renum.显示成功.ToString();
            var list = _roleRepository.GetAll().Where(x => !x.IsDeleted);
            result.Data= _mapper.Map<List<RoleDto>>(list);
            return Task.FromResult(result);
        }
    }
}
